package com.booby.generic;

/**
 * 功能描述: 泛型类
 *
 * @author: lizt
 * @date: 2020/8/14 09:35
 **/

public class GenericClass<T> {

   private T param;

    public GenericClass(T param) {
        this.param = param;
    }

    public GenericClass() {
    }

    public T getParam() {
        return param;
    }

    public void setParam(T param) {
        this.param = param;
    }

    public void sayHi(){
        System.out.println("Hi:" + param);
    }

    public static void main(String[] args) {
        String paramString = "泛型String";
        Integer paramsInt = 111;
//        GenericClass clazz = new GenericClass();
        //可以设置 String 和 Integer 类型
//        clazz.setParam(paramString);
//        clazz.setParam(paramsInt);
        // 泛型赋值类型必须和初始化类型一致
        // 初始化类型和变量类型都为String
        GenericClass<String> clazz = new GenericClass<>(paramString);
        // 初始化类型和变量类型不一致，编译无法通过
//        GenericClass<Integer> clazzA = new GenericClass<>(paramString);
        // 初始化类型和变量类型都为Integer
        GenericClass<Integer> clazzB = new GenericClass<>(paramsInt);
        // 设置参数也失败
//        clazzB.setParam(paramString);
        clazz.sayHi();
    }
}
